﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.DTO;
using Taurus.Platform.XML;
using System.Xml.Linq;

namespace Taurus.XML
{
    class ComponentPersistence : BasePersistence<ComponentDTO>
    {
        public override string FileName
        {
            get { return "TaurusComponent2.xml"; }
        }

        public override XElement GetEl(ComponentDTO item)
        {
            return (from el in Root.Elements("Component") where el.Attribute("UniqueID").Value == item.UniqueID select el).FirstOrDefault();
        }

        public override ComponentDTO GetObject(string id)
        {
            return XChange(GetEl(new ComponentDTO() { UniqueID = id }));
        }

        protected override System.Xml.Linq.XElement XChange(ComponentDTO item)
        {
            XElement el = new XElement("Component");
            el.SetAttributeValue("UniqueID", item.UniqueID);
            el.SetAttributeValue("Version", item.Version);
            el.SetAttributeValue("Name", item.Name);
            return el;
        }

        protected override ComponentDTO XChange(System.Xml.Linq.XElement item)
        {
            ComponentDTO dto = null;
            if (item!=null)
            {
                dto = new ComponentDTO();
                dto.Name = item.Attribute("Name").Value;
                dto.UniqueID = item.Attribute("UniqueID").Value;
                dto.Version = item.Attribute("Version").Value;
            }
            return dto;
        }
    }
}
